/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.camachosanchez.mechanics;

//import com.sun.image.codec.jpeg.JPEGCodec;
//import com.sun.image.codec.jpeg.JPEGImageDecoder;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.TexturePaint;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.InputStream;
import javax.imageio.ImageIO;

/**
 *
 * @author Rolando
 */
public class LlantaES implements Cloneable {

    private int w_contenedor;
    private int h_contenedor;
    private int origen;
    private EjeES eje;
    private int x;
    private int y;
    private BufferedImage llanta;

    public LlantaES(EjeES eje, int w_contenedor, int h_contenedor) {
        try {
            this.eje = eje;
            this.x = eje.getX();
            this.y = eje.getY();
            this.w_contenedor = w_contenedor;
            this.h_contenedor = h_contenedor;
            InputStream in = getClass().getResourceAsStream("/com/camachosanchez/recursos/tire2.jpg");
//            JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(in);
//            llanta = decoder.decodeAsBufferedImage();
            llanta = ImageIO.read(in);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public EjeES getEje() {
        return eje;
    }

    public void setEje(EjeES eje) {
        this.eje = eje;
    }

    public int getH_contenedor() {
        return h_contenedor;
    }

    public void setH_contenedor(int h_contenedor) {
        this.h_contenedor = h_contenedor;
    }

    public int getOrigen() {
        return origen;
    }

    public void setOrigen(int origen) {
        this.origen = origen;
    }

    public int getW_contenedor() {
        return w_contenedor;
    }

    public void setW_contenedor(int w_contenedor) {
        this.w_contenedor = w_contenedor;
    }

    public void graficar(Graphics2D graphics) {
        try {
            int ancho = eje.getAncho();
            int alto = eje.getAlto();
            x = x < 7 ? 7 : x;
            y = y < 7 ? 7 : y;
            x = x + ancho + 7 > w_contenedor ? w_contenedor - ancho - 7 : x;
            y = y + alto + 7 > h_contenedor ? h_contenedor - alto - 7 : y;
            graphics.setColor(Color.black);
            RoundRectangle2D r = new RoundRectangle2D.Float(x + 1, y + 1, ancho - 2, alto - 2, 7, 7);
            Rectangle2D tr = new Rectangle2D.Double(x + 1, y + 1, ancho - 2, alto - 2);
            TexturePaint tp = new TexturePaint(llanta, tr);
            Paint old = graphics.getPaint();
            graphics.setPaint(tp);
            graphics.fill(r);
            graphics.setPaint(old);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public Object clone() {
        Object clone = null;
        try {
            clone = super.clone();
        } catch (CloneNotSupportedException e) { // No deberia suceder
            e.printStackTrace();
        }
        return clone;
    }
}
